<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 14-9-4
 * Time: 上午9:32
 */
/**
 * TODO:根据子票ID查找出套票的ID
 *
 * @param integer $pid 子票的pid（uu_products 表ID）
 * @param integer $format 返回数据类型（0套票lid，1套票pid,2pid=>lid）
 * @return array|int|string 套票产品lid(uu_land 表 ID)
 */
function getMainId($pid, $format=0 )
{
    $products = array();
    $output = array();
    foreach (glob( dirname(__FILE__) . "/data/p_{$pid}_*.inc.php", GLOB_BRACE) as $filename) {
        $output[] = include $filename;
    }
    foreach ($output as $prod) {
        $products[$pid][] = $prod;
    }
    $lid_key  = array();
    $pid_key  = array();
    $pid_lid  = array();
    foreach ($products as $key=>$pros) {
        if ($key == $pid) {
            foreach ($pros as $product) {
                $lid_key[] = $product['lid'];
                $pid_key[] = $product['pid'];
                $pid_lid[$product['pid']] = $product['lid'] . '|'. $product['num'];
            }
            break;
        }
    }
    if ($format==1) {
        return $pid_key;
    }
    else if ($format==2) {
        return $pid_lid;
    }
    else {
        return $lid_key;
    }
}